// import {
//   getAppletSelectList,
//   getAreaRegionType,
//   getEnterpriseSelect,
//   getGridListByAreaCode,
//   getHouseTree,
//   getPartyOrgSelect,
//   getPartyPersonSelect,
//   getYardSelect,
//   teamPeopleSelect,
// } from '../api/index.js';
// // 小区 - 下拉列表
// const getYardSelectList = async (area_code) => {
//   const res = await getYardSelect({ area_code });
//   const data = res ? res.data || [] : [];
//   return data.map((item) => {
//     return Object.assign({}, item, {
//       label: item.yard_name,
//       value: item.yard_id,
//     });
//   });
// };
// export const getYardList = getYardSelectList;
// // 所属企业
// const getEnterprise = singlePromise(async (area_code) => {
//   const res = await getEnterpriseSelect({ area_code: area_code });
//   const data = res ? res.data || [] : [];
//   return data.map((item) => {
//     return Object.assign(
//       {},
//       {
//         label: item.name,
//         value: item.id,
//         key: item.id,
//       }
//     );
//   });
// });
// export const getEnterpriseList = getEnterprise;
// // 综治人员列表
// const getTeamSelect = singlePromise(async (unit_code) => {
//   const data = handleCommonResult(await teamPeopleSelect({ unit_code: unit_code }));
//   return data.map((item) => Object.assign({}, item, { label: item.name, value: item.id, key: item.id }));
// });
// export const getTeamPeopleList = getTeamSelect;
// // 党组织列表
// const getPartyOrg = singlePromise(async (params) => {
//   const data = handleCommonResult(await getPartyOrgSelect(params));
//   return data.map((item) => Object.assign({}, item, { label: item.name, value: item.id }));
// });
// export const getPartyOrgList = getPartyOrg;
// // 党员列表
// const getPartyPerson = singlePromise(async (party_org_id) => {
//   const data = handleCommonResult(await getPartyPersonSelect({ party_org_id }));
//   return data.map((item) => Object.assign({}, { label: item.name, value: item.id }));
// });
// export const getPartyPersonSelectList = getPartyPerson;
// // 小程序列表
// const getApplet = singlePromise(async (params) => {
//   const data = handleCommonResult(await getAppletSelectList(params));
//   return data.map((item) => Object.assign({}, item, { label: item.applet_name, value: item.applet_id }));
// });
// export const appletSelectList = getApplet;
//
// // 区域类型
// export const getAreaType = singlePromise(async (params) => {
//   const data = handleCommonResult(await getAreaRegionType(params));
//   return data.map((item) => Object.assign({}, item, { label: item.name, value: item.value }));
// });
//
// // 酒店列表
// const getHotel = singlePromise(async (params) => {
//   const data = handleCommonResult(await getHotelSelectList(params));
//   return data.map((item) => Object.assign({}, item, { label: item.hotel_name, value: item.hotel_id }));
// });
// export const hotelSelectList = getHotel;
// // 根据area_code获取网格
// export const getGridList = async (area_code) => {
//   const data = handleCommonResult(await getGridListByAreaCode({ area_code: area_code }));
//   if (!data || !data.length) return [];
//   return data.map((item) => Object.assign({}, item, { label: item.area_name, value: item.area_code }));
// };
//
// export class HouseTreeData {
//   constructor(area_code, option) {
//     const opt = {
//       fetchSuccess: () => {},
//     };
//     this.option = Object.assign({}, opt, option);
//     this.dataList = [];
//     this.update(area_code);
//   }
//   async getDataAsync() {
//     const { area_code } = this;
//     const res = await getHouseTree({ area_code });
//     const data = res ? res.data || [] : [];
//     if (data && data[0] && data[0].nodes && data[0].nodes.length) {
//       Vue.set(this, 'dataList', replaceFields(data[0].nodes, { label: 'text', value: 'id' }, 'nodes'));
//       if (this.option && typeof this.option.fetchSuccess === 'function') {
//         this.option.fetchSuccess(this.dataList);
//       }
//     }
//   }
//   getChildren(id) {
//     if (!id) return [];
//     const data = recursive.breadthFirst(this.dataList, {
//       children: 'nodes',
//       predicate: (item) => item.value === id,
//     });
//     return data && data.nodes && data.nodes.length ? data.nodes : [];
//   }
//   update(area_code) {
//     if (area_code) {
//       this.area_code = area_code;
//       this.getDataAsync().catch();
//     }
//   }
// }

// 区域类型
import { useCacheRequest } from '../plugins/useRequest.js';
import { handleCommonRes } from '../utils/resolver.js';
import { getAreaRegionType } from '../api/index.js';

export const getAreaType = async (params) => {
  return await useCacheRequest(async (params) => {
    const res = await handleCommonRes(getAreaRegionType, params, { defaultValue: {} });
    const list = res.map((item) => {
      return Object.assign({}, item, { label: item.name, value: item.value });
    });
    const data = res.reduce((res, item) => {
      return Object.assign(res, { [item.value]: item.name });
    }, {});
    return { list, data };
  }, params);
};
